//声明荣耀数组
var honors = new Array();
//var honorDic = [{2:0},{4:1},{8:2},{16:3},{32:4},{64:5},{128:6},{256:7},{512:8},{1024:9},{2048:10},{4096:11},{8192:12}];
var honorDic = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

//默认2048
var textArr = ["2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"];

function changeToNormal() {
	textArr = ["2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"];
	init();
}
//程序员2048
function changeToCoder() {
	textArr = ["小白", "实习生", "程序猿", "项目经理", "架构师", "技术经理", "高级经理", "技术总监", "副总裁", "CTO", "总裁", "太高了", "好高啊"];
	init();
}
//恋爱版2048
function changeToLove() {
	textArr = ["搭讪", "暧昧", "约会", "表白", "恋爱", "牵手", "拥抱", "接吻", "XXOO", "求婚", "小恩爱", "相濡以沫", "白头偕老"];
	init();
}
//图片版2048
function changeToPic() {
	textArr = ["img/p1.png", "img/p2.png", "img/p3.png", "img/p4.png", "img/p5.png", "img/p6.png", "img/p7.png", "img/p8.png", "img/p9.png", "img/p10.png", "img/p11.png", "img/p12.png", "img/p13.png"];
	init();
}
//判断内容是不是字符串
function textIsAStr(str) {
	if(str == "2" || str == "4" || str == "8" || str == "16" || str == "32" || str == "64" || str == "128" || str == "256" || str == "512" || str == "1024" || str == "2048" || str == "4096" || str == "8192") {
		return false;
	} else {
		return true;
	}
}
//得到距离顶部的位置
function getPostionTop(i, j) {
	return 20 + 120 * i;
}
//得到距离左边的位置
function getPostionLeft(i, j) {
	return 20 + 120 * j;
}
//判断当前cell是否为空的
function noSpace(board) {
	for(var i = 0; i < 4; i++) {
		for(var j = 0; j < 4; j++) {
			if(board[i][j] == 0) {
				return false;
			}
		}
	}
	return true;
}
//判断是否能移动
function noMove(board) {
	if(canMoveLeft(board) || canMoveRight(board) || canMoveUp(board) || canMoveDown(board)) {
		return false;
	}
	return true;
}
//获得cell的背景颜色
function getNumberBackgroundColor(number) {
	switch(number) {
		case 2:
			return "#eee4da";
			break;
		case 4:
			return "#ede0c8";
			break;
		case 8:
			return "#f2b179";
			break;
		case 16:
			return "#f59563";
			break;
		case 32:
			return "#f67c5f";
			break;
		case 64:
			return "#f65e3b";
			break;
		case 128:
			return "#edcf72";
			break;
		case 256:
			return "#edcc61";
			break;
		case 512:
			return "#9c0";
			break;
		case 1024:
			return "#33b5e5";
			break;
		case 2048:
			return "#09c";
			break;
		case 4096:
			return "#a6c";
			break;
		case 8192:
			return "#93c";
			break;
	}

	return "black";
}
//获得cell字体的颜色
function getNumberColor(number) {
	if(number <= 4) {
		return "#776e65";
	}
	return "white";
}
//获得cell的内容
function getNumberText(number) {
	switch(number) {
		case 2:
			return textArr[0];
			break;
		case 4:
			return textArr[1];
			break;
		case 8:
			return textArr[2];
			break;
		case 16:
			return textArr[3];
			break;
		case 32:
			return textArr[4];
			break;
		case 64:
			return textArr[5];
			break;
		case 128:
			return textArr[6];
			break;
		case 256:
			return textArr[7];
			break;
		case 512:
			return textArr[8];
			break;
		case 1024:
			return textArr[9];
			break;
		case 2048:
			return textArr[10];
			break;
		case 4096:
			return textArr[11];
			break;
		case 8192:
			return textArr[12];
			break;
	}

	return "black";
}
//判断能否往左边移动
function canMoveLeft(board) {
	for(var i = 0; i < 4; i++) {
		for(var j = 1; j < 4; j++) {
			if(board[i][j] != 0) {
				if(board[i][j - 1] == 0 || board[i][j - 1] == board[i][j]) {
					return true;
				}
			}
		}
	}
	return false;
}
//判断是否能往右移动
function canMoveRight(board) {
	for(var i = 0; i < 4; i++) {
		for(var j = 2; j >= 0; j--) {
			if(board[i][j] != 0) {
				if(board[i][j + 1] == 0 || board[i][j + 1] == board[i][j]) {
					return true;
				}
			}
		}
	}
	return false;
}
//判断是否能往上移动
function canMoveUp(board) {
	for(var j = 0; j < 4; j++) {
		for(var i = 1; i < 4; i++) {
			if(board[i][j] != 0) {
				if(board[i - 1][j] == 0 || board[i - 1][j] == board[i][j]) {
					return true;
				}
			}
		}
	}
	return false;
}
//判断是否能往下移动
function canMoveDown(board) {
	for(var j = 0; j < 4; j++) {
		for(var i = 2; i >= 0; i--) {
			if(board[i][j] != 0) {
				if(board[i + 1][j] == 0 || board[i + 1][j] == board[i][j]) {
					return true;
				}
			}
		}
	}
	return false;
}
//水平范围没有阻碍cell
function noBlockHorizontal(row, col1, col2, board) {
	for(var i = col1 + 1; i < col2; i++) {
		if(board[row][i] != 0) {
			return false;
		}
	}
	return true;
}
//垂直范围内没有阻碍cell
function noBlockVertical(col, row1, row2, board) {
	for(var i = row1 + 1; i < row2; i++) {
		if(board[i][col] != 0) {
			return false;
		}
	}
	return true;
}
//更新用户排名
function sortScore(userNames) {
	for(var i = 0; i < userNames.length; i++) {
		for(var j = 0; j < userNames.length - i - 1; j++) {
			if(userNames[j][1] > userNames[j + 1][1]) {
				var temp = userNames[j];
				userNames[j] = userNames[j + 1];
				userNames[j + 1] = temp;
			}
		}
	}

	var firstSpan = $("#price-0 span");
	var secondSpan = $("#price-1 span");
	var thirdSpan = $("#price-2 span");

	if(userNames.length == 1) {
		firstSpan.html(userNames[0][1] + "-" + userNames[0][0]);
	} else if(userNames.length == 2) {
		firstSpan.html(userNames[0][1] + "-" + userNames[0][0]);
		secondSpan.html(userNames[1][1] + "-" + userNames[1][0]);
	} else {
		firstSpan.html(userNames[0][1] + "-" + userNames[0][0]);
		secondSpan.html(userNames[1][1] + "-" + userNames[1][0]);
		thirdSpan.html(userNames[2][1] + "-" + userNames[2][0]);
	}
}
//判断是否能解锁成就
function canUnlockHonor(board) {
	for(var i = 0; i < 4; i++) {
		for(var j = 0; j < 4; j++) {
			switch(board[i][j]) {
				case 2:
					if(!hasThisElement(2)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[0]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 4:
					if(!hasThisElement(4)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[1]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 8:
					if(!hasThisElement(8)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[2]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 16:
					if(!hasThisElement(16)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[3]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 32:
					if(!hasThisElement(32)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[4]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 64:
					if(!hasThisElement(64)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[5]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 128:
					if(!hasThisElement(128)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[6]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 256:
					if(!hasThisElement(256)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[7]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 512:
					if(!hasThisElement(512)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[8]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 1024:
					if(!hasThisElement(1024)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[9]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 2048:
					if(!hasThisElement(2048)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[10]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 4096:
					if(!hasThisElement(4096)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[11]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				case 8192:
					if(!hasThisElement(8192)) {
						honors.push(board[i][j]);
						showHonorAnimation(honorDic[12]);
						var honorMusic = document.getElementById("honorMusic");
						honorMusic.play();
					}
					break;
				default:
					break;
			}
		}
	}
	//	return false;
}
//判断荣耀数组中是否存在
function hasThisElement(num) {
	console.log(honors);
	for(var i = 0; i < honors.length; i++) {
		if(honors[i] == num) {
			return true;
		}
	}
	return false;
}